package com.miage.kangourou.client.calendar;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.miage.kangourou.client.GetListesService;
import com.miage.kangourou.client.GetListesServiceAsync;
import com.miage.kangourou.client.beans.Utilisateur;

public class PopUpConnection {

	private final GetListesServiceAsync getListesService = GWT
			.create(GetListesService.class);
	final PopupPanel popUpConnection = new PopupPanel(true);

	KangourouPanel context;

	@SuppressWarnings("deprecation")
	public PopupPanel CreatePopUpConnection(final KangourouPanel context) {

		this.context = context;

		final VerticalPanel popUpPanel = new VerticalPanel();
		HorizontalPanel userNamePanel = new HorizontalPanel();
		HorizontalPanel passwordPanel = new HorizontalPanel();
		HorizontalPanel submitPanel = new HorizontalPanel();
		Label usernameLabel = new Label("Nom d'utilisateur :", false);
		final Label infoLabel = new Label();
		infoLabel.setStyleName("red");
		final TextBox username = new TextBox();
		Label passwordLabel = new Label("Mot de passe :", false);
		final PasswordTextBox password = new PasswordTextBox();
		Button submit = new Button("Se connecter");
		Button cancel = new Button("Annuler");
		Hyperlink passwordForgotten = new Hyperlink("Mot de passe oublié",
				false, "");
		userNamePanel.add(usernameLabel);
		userNamePanel.setCellHorizontalAlignment(usernameLabel,
				HasHorizontalAlignment.ALIGN_RIGHT);
		userNamePanel.setCellWidth(usernameLabel, "200px");
		userNamePanel.setCellVerticalAlignment(usernameLabel,
				HasVerticalAlignment.ALIGN_MIDDLE);
		userNamePanel.setHeight("30px");
		userNamePanel.add(username);
		userNamePanel.setSpacing(5);
		userNamePanel.setCellHorizontalAlignment(username,
				HasHorizontalAlignment.ALIGN_LEFT);
		passwordPanel.add(passwordLabel);
		passwordPanel.setCellHorizontalAlignment(passwordLabel,
				HasHorizontalAlignment.ALIGN_RIGHT);
		passwordPanel.setCellVerticalAlignment(passwordLabel,
				HasVerticalAlignment.ALIGN_MIDDLE);
		passwordPanel.setCellWidth(passwordLabel, "200px");
		passwordPanel.setHeight("30px");
		passwordPanel.add(password);
		passwordPanel.setSpacing(5);
		passwordPanel.setCellHorizontalAlignment(password,
				HasHorizontalAlignment.ALIGN_LEFT);
		submitPanel.add(submit);
		submitPanel.setCellHorizontalAlignment(submit,
				HasHorizontalAlignment.ALIGN_RIGHT);
		submitPanel.setCellWidth(submit, "200px");
		submitPanel.add(cancel);
		submitPanel.setCellHorizontalAlignment(cancel,
				HasHorizontalAlignment.ALIGN_LEFT);
		submitPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		submitPanel.setCellWidth(cancel, "200px");
		submitPanel.setSpacing(5);
		submitPanel.setHeight("30px");
		popUpPanel.add(userNamePanel);
		popUpPanel.add(passwordPanel);
		popUpPanel.add(submitPanel);
		popUpPanel.add(passwordForgotten);
		popUpPanel.add(infoLabel);
		popUpPanel.setCellHeight(passwordForgotten, "30px");
		popUpPanel.setCellHorizontalAlignment(passwordForgotten,
				HasHorizontalAlignment.ALIGN_CENTER);
		popUpConnection.setWidget(popUpPanel);
		popUpConnection.setWidth("400px");
		popUpConnection.setHeight("200px");
		popUpConnection.center();
		username.setFocus(true);
		username.setCursorPos(0);
		final HorizontalPanel passwordForgottenPanel = new HorizontalPanel();
		final Label errorEmail = new Label("Email inexistant ", false);
		final Label emailLabel = new Label("Email :", false);
		final TextBox email = new TextBox();
		final Button submitB = new Button("Ok");
		passwordForgottenPanel.add(emailLabel);
		passwordForgottenPanel.add(email);
		passwordForgottenPanel.add(submitB);
		errorEmail.setVisible(false);
		passwordForgottenPanel.setVisible(false);
		passwordForgottenPanel.setCellHorizontalAlignment(emailLabel,
				HasHorizontalAlignment.ALIGN_RIGHT);
		passwordForgottenPanel.setCellVerticalAlignment(emailLabel,
				HasVerticalAlignment.ALIGN_MIDDLE);
		passwordForgottenPanel.setCellHorizontalAlignment(email,
				HasHorizontalAlignment.ALIGN_CENTER);
		passwordForgottenPanel.setCellVerticalAlignment(email,
				HasVerticalAlignment.ALIGN_MIDDLE);
		passwordForgottenPanel.setCellHorizontalAlignment(submitB,
				HasHorizontalAlignment.ALIGN_LEFT);
		passwordForgottenPanel.setCellVerticalAlignment(submitB,
				HasVerticalAlignment.ALIGN_MIDDLE);
		passwordForgottenPanel
				.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		passwordForgottenPanel
				.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		passwordForgottenPanel.setWidth("100%");
		passwordForgottenPanel.setCellWidth(emailLabel, "20%");
		passwordForgottenPanel.setCellWidth(email, "60%");
		passwordForgottenPanel.setCellWidth(submitB, "20%");
		email.setWidth("95%");
		passwordForgottenPanel.setHeight("30px");
		passwordForgottenPanel.setSpacing(5);
		popUpPanel.add(passwordForgottenPanel);

		submit.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				final AsyncCallback<Utilisateur> util = new AsyncCallback<Utilisateur>() {

					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Erreur lors de l'appel serveur "
								+ caught.getMessage());
					}

					@Override
					public void onSuccess(Utilisateur result) {
						context.utilisateurEnCours = result;
						if (result.isEmpty()) {
							infoLabel
									.setText("Erreur , verifiez vos identifiants");
						} else {
							popUpConnection.hide();
							context.refreshAfterLogin();
						}

					}
				};
				getListesService.login(username.getText(), Util.toMd5(password
						.getText()), util);

			}

		});

		password.addKeyPressHandler(new KeyPressHandler() {

			@Override
			public void onKeyPress(KeyPressEvent event) {
				if (event.getCharCode() == KeyCodes.KEY_ENTER) {
					final AsyncCallback<Utilisateur> util = new AsyncCallback<Utilisateur>() {

						@Override
						public void onFailure(Throwable caught) {
							Window.alert("Erreur lors de l'appel serveur "
									+ caught.getMessage());
						}

						@Override
						public void onSuccess(Utilisateur result) {
							context.utilisateurEnCours = result;
							if (result.isEmpty()) {
								infoLabel
										.setText("Erreur , verifiez vos identifiants");
							} else {
								popUpConnection.hide();
								context.refreshAfterLogin();

							}

						}
					};
					getListesService.login(username.getText(), Util
							.toMd5(password.getText()), util);
				}

			}
		});

		passwordForgotten.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {

				email.setValue(username.getValue());
				passwordForgottenPanel.setVisible(true);
				email.setFocus(true);
				email.setCursorPos(0);
			}
		});

		cancel.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				popUpConnection.hide();

			}

		});
		submitB.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {

			}

		});

		return popUpConnection;
	}
}
